/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { message, Button, Typography } from 'antd';
import type { Fields, RefuseModalProps } from '@/components/_pages/jdk/order/_utils/type';
import { FieldList } from '../../_utils/config';
import { editOrderDelivery } from '@/services/jdk/order';
import ProForm, {
  ModalForm,
  ProFormText,
  ProFormDigit,
  ProFormSelect,
  ProFormDependency,
  ProFormCheckbox,
} from '@ant-design/pro-form';
import { createTips, getShowStatus, createFormFields } from '../../_utils';
import { deliveryValueEnum, typeValueEnum } from '../../_utils/config';

const { Paragraph } = Typography;
const RefuseModal: React.FC<RefuseModalProps> = ({ onReload, task_order_id: order_id }) => {
  const onSubmit = async (values: any) => {
    const { total_fee, real_fee, is_fn_school: isFnSchool } = values;
    const is_fn_school = isFnSchool ? 1 : 0;
    if (1 * real_fee > 1 * total_fee) {
      message.error('实际价不能高于原价');
      return false;
    }
    return editOrderDelivery({ ...values, order_id, is_fn_school }).then(async (res) => {
      const { code, msg } = res;
      if (code == 0) {
        message.success('操作成功');
        onReload();
        return true;
      } else {
        message.error(msg);
        return false;
      }
    });
  };

  return (
    <ModalForm
      title='手动结单'
      trigger={
        <Button type='primary' size='small'>
          手动结单
        </Button>
      }
      width={520}
      layout='horizontal'
      modalProps={{
        destroyOnClose: true,
      }}
      onFinish={onSubmit}
    >
      <Paragraph type='danger'>
        注：仅支持对待接单、取货中、配送中、异常单中未取消配送的订单进行操作
      </Paragraph>
      <ProFormText name='time' {...createTips('时间')} />
      <ProFormSelect
        name='delivery_type'
        valueEnum={deliveryValueEnum}
        {...createTips('运力类型', 'select')}
      />
      <ProFormSelect name='type' valueEnum={typeValueEnum} {...createTips('订单类型', 'select')} />
      <ProFormDependency name={['type']}>
        {({ type: key }) => {
          return (
            <ProForm.Group>
              {createFormFields(FieldList).map((item: Fields) => {
                const { id, node, keys, rules, ...rest } = item;
                return getShowStatus(keys, key) ? (
                  node === 'input' ? (
                    <ProFormText key={id} {...rest} rules={rules} />
                  ) : node === 'select' ? (
                    <ProFormSelect key={id} {...rest} rules={rules} />
                  ) : node === 'digit' ? (
                    <ProFormDigit key={id} {...rest} rules={rules} />
                  ) : node === 'checkbox' ? (
                    <ProFormCheckbox key={id} {...rest} rules={rules} />
                  ) : null
                ) : null;
              })}
            </ProForm.Group>
          );
        }}
      </ProFormDependency>
    </ModalForm>
  );
};

export default RefuseModal;
